<template>
    <div>
      <el-form data-v-step="1" :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
        <el-form-item prop="name">
               <!-- <el-input size="small" v-model="searchForm.name" placeholder="姓名" clearable></el-input> -->
              <el-autocomplete
                class="input_height inline-input"
                v-model="searchForm.name"
                :fetch-suggestions="queryName"
                placeholder="姓名"
                @select="handleSelect"
              ></el-autocomplete>
        </el-form-item>
        <el-form-item prop="institute">
          <el-autocomplete
            class="inline-input"
            v-model="searchForm.institute"
            :fetch-suggestions="queryInstitute"
            placeholder="学院"
            @select="handleSelect"
            :disabled="role > 1"
          ></el-autocomplete>
               <!-- <el-input size="small" v-model="searchForm.institute" placeholder="学院" :disabled="role > 1" clearable></el-input> -->
        </el-form-item>
        <el-form-item prop="grade">
               <el-input size="small" v-model="searchForm.grade" placeholder="年级" :disabled="role > 3" clearable></el-input>
        </el-form-item>
        <el-form-item prop="classe">
               <el-input size="small" v-model="searchForm.classe" placeholder="班级" :disabled="role > 3" clearable></el-input>
        </el-form-item>
        <el-form-item prop="teacher">
          <el-autocomplete
            class="inline-input"
            v-model="searchForm.teacher"
            :fetch-suggestions="queryTeacher"
            placeholder="辅导员老师"
            @select="handleSelect"
          ></el-autocomplete>
               <!-- <el-input size="small" v-model="searchForm.teacher" placeholder="辅导员老师" :disabled="role > 2" clearable></el-input> -->
        </el-form-item>
        <el-form-item prop="address">
               <el-input size="small " v-model="searchForm.address" placeholder="学号" clearable></el-input>
        </el-form-item>
        <el-select v-model="searchForm.evaluate" placeholder="是否评价">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-form-item prop="studentId">
               <el-input size="small " v-model="searchForm.studentId" placeholder="学号" clearable></el-input>
        </el-form-item>
        <el-form-item prop="state">
               <el-input size="small" v-model="searchForm.state" placeholder="学籍状态" clearable></el-input>
        </el-form-item>
        <el-form-item prop="telphone">
               <el-input size="small" v-model="searchForm.telphone" placeholder="家长联系方式" :disabled="role > 3" clearable></el-input>
        </el-form-item>
        <el-form-item prop="relationship">
               <el-input size="small" v-model="searchForm.relationship" placeholder="与学生的关系" :disabled="role > 3" clearable></el-input>
        </el-form-item>
            <!-- 搜索框-->
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <el-dialog  title="导入Excel" :visible.sync="isImportCollapse">
          <el-form size="small" :inline="true" v-show="isImportCollapse"  ref="importForm">
             <el-form-item>
              <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
             </el-form-item>
             <el-form-item prop="loginName">
                <el-upload
                  class="upload-demo"
                  :action="`${this.$http.BASE_URL}/studentevaluate/studentEvaluate/import`"
                  :on-success="uploadSuccess"
                   :show-file-list="true">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
                </el-upload>
            </el-form-item>
          </el-form>
      </el-dialog>
      <el-row>
        <!-- <el-button v-if="hasPermission('studentevaluate:studentEvaluate:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button> -->
        <el-button v-if="hasPermission('studentevaluate:studentEvaluate:imp')" type="primary" size="small" icon="el-icon-circle-plus-outline" @click="imp()" plain>一键导入
        </el-button>
        <el-button v-if="hasPermission('studentevaluate:studentEvaluate:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button>
        <el-button v-if="hasPermission('studentevaluate:studentEvaluate:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button>
        <el-button v-if="hasPermission('studentevaluate:studentEvaluate:imp')" type="danger"   size="small" icon="el-icon-delete" @click="delall()"
                  plain>一键删除
        </el-button>
        <el-button v-if="hasPermission('studentevaluate:studentEvaluate:del')" type="danger"   data-v-step="2" size="small" icon="el-icon-delete" @click="reEvaluate()"
                  plain>打回评价
        </el-button>
        <el-button type="primary" @click="guide()" size="small">开始教学</el-button>
        <!-- <el-button type="primary" @click="test()" size="small">test</el-button> -->
<!--        <el-button size="small" icon="el-icon-delete" @click="createfa()"
                  plain>创建
        </el-button> -->
        <v-tour name="myTour" :steps="steps" :options="myOptions">
          <template slot-scope="tour">
              <transition name="fade">
                <v-step
                  v-if="tour.steps[tour.currentStep]"
                  :key="tour.currentStep"
                  :step="tour.steps[tour.currentStep]"
                  :previous-step="tour.previousStep"
                  :next-step="tour.nextStep"
                  :stop="tour.stop"
                  :skip="tour.skip"
                  :is-first="tour.isFirst"
                  :is-last="tour.isLast"
                  :labels="tour.labels"
                >
                  <template v-if="tour.currentStep === 0">
                    <div slot="actions">
                      <button @click="tour.previousStep" class="btn btn-primary">上一步</button>
                      <button @click="tour.nextStep" class="btn btn-primary" v-show="isSearchCollapse === true">下一步</button>
                    </div>
                  </template>
                </v-step>
              </transition>
            </template>
        </v-tour>
        <el-button-group class="pull-right">
          <el-button
            type="default"
            size="small"
            icon="el-icon-search"
            data-v-step="0"
            @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
          </el-button>
            <el-button v-if="hasPermission('studentevaluate:studentEvaluate:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse"></el-button>
            <el-button v-if="hasPermission('studentevaluate:studentEvaluate:export')" type="default" size="small" icon="el-icon-download" data-v-step="3" title="导出" @click="dialogVisible = true"></el-button>
            <!-- title="提示" -->
            <el-dialog
              :visible.sync="dialogVisible"
              width="30%"
              :before-close="handleClose"
              style="margin-left: 0px;">
              <span>导出 <span style="color: red;">{{searchForm.institute}} {{ searchForm.grade }} {{searchForm.classe}}</span> 数据</span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="exportExcel()">确 定</el-button>
              </span>
            </el-dialog>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
      border
      size="medium"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
    <el-table-column
        prop="name"
        show-overflow-tooltip
        sortable="custom"
        label="姓名">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('studentevaluate:studentEvaluate:edit')" @click="edit(scope.row.id)">{{scope.row.name}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('studentevaluate:studentEvaluate:view')"  @click="view(scope.row.id)">{{scope.row.name}}</el-link>
              <span v-else>{{scope.row.name}}</span>
            </template>
      </el-table-column>
    <el-table-column
        prop="institute"
        show-overflow-tooltip
        sortable="custom"
        label="学院">
        <template slot-scope="scope">
          <el-link  type="primary" :underline="false" v-if="hasPermission('studentevaluate:studentEvaluate:view')" @click="clickQuery(scope.row.institute)">{{scope.row.institute}}</el-link>
          <span v-else>{{scope.row.institute}}</span>
        </template>
      </el-table-column>
    <el-table-column
        prop="grade"
        show-overflow-tooltip
        sortable="custom"
        label="年级">
      </el-table-column>
    <el-table-column
        prop="classe"
        show-overflow-tooltip
        sortable="custom"
        label="班级">
      </el-table-column>
      <el-table-column
          prop="teacher"
          show-overflow-tooltip
          sortable="custom"
          label="当前老师">
          <template slot-scope="scope">
            <el-link  type="primary" :underline="false" v-if="hasPermission('studentevaluate:studentEvaluate:view')" @click="clickTeacher(scope.row.teacher)">{{scope.row.teacher}}</el-link>
            <span v-else>{{scope.row.teacher}}</span>
          </template>
        </el-table-column>
    <el-table-column
        prop="studentId"
        show-overflow-tooltip
        sortable="custom"
        label="学号">
      </el-table-column>
    <el-table-column
        prop="state"
        show-overflow-tooltip
        sortable="custom"
        label="学籍状态">
      </el-table-column>
    <el-table-column
        prop="relationship"
        show-overflow-tooltip
        sortable="custom"
        label="与学生的关系">
      </el-table-column>
      <el-table-column
          prop="telphone"
          show-overflow-tooltip
          sortable="custom"
          label="家长联系方式">
        </el-table-column>
    <el-table-column
        prop="evaluate"
        show-overflow-tooltip
        sortable="custom"
        label="是否评价">
      </el-table-column>
    <el-table-column
        prop="signature"
        show-overflow-tooltip
        sortable="custom"
        label="手写签名">
        <template slot-scope="scope" v-if="scope.row.signature">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.signature"
              fit="fill"
              >
            </el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="address"
          show-overflow-tooltip
          sortable="custom"
          label="家庭地址">
        </el-table-column>
    <!-- <el-table-column
        prop="remarks"
        show-overflow-tooltip
        sortable="custom"
        label="备注信息">
      </el-table-column> -->
      <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        :key="Math.random()"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <!-- v-if="hasPermission('studentevaluate:studentEvaluate:view')" -->
          <el-button  type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('studentevaluate:studentEvaluate:edit')&&isupdate === true" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('studentevaluate:studentEvaluate:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
    <StudentEvaluateForm  ref="studentEvaluateForm" @refreshDataList="refreshList"></StudentEvaluateForm>
  </div>
</template>

<script>
  import StudentEvaluateForm from './StudentEvaluateForm'
  import pinyin from 'pinyin-match'
  export default {
    data () {
      return {
        searchForm: {
          name: '',
          institute: '',
          grade: '',
          classe: '',
          teacher: '',
          address: '',
          studentId: '',
          evaluate: '',
          telphone: '',
          relationship: '',
          state: ''
        },
        steps: [
          {
            target: '[data-v-step="0"]',  // We're using document.querySelector() under the hood
            content: `请先点击该按钮再点击下一步`,
            params: {
              placement: 'top'
            }
          },
          {
            target: '[data-v-step="1"]',
            content: '可以在这里查询您想查询的信息',
            params: {
              placement: 'top'
            }
          },
          {
            target: '[data-v-step="2"]',
            content: '可以选择学生前面的√来点击这里打回评价内容',
            params: {
              placement: 'top'
            }
          },
          {
            target: '[data-v-step="3"]',
            content: '可以导出按当前查询的内容',
            params: {
              placement: 'top'
            }
          }
        ],
        myOptions: {
          useKeyboardNavigation: false,
          labels: {
            buttonSkip: '跳过教学',
            buttonPrevious: '上一步',
            buttonNext: '下一步',
            buttonStop: '结束教程'
          },
          highlight: true
        },
        dialogVisible: false,
        nameList: [], // 把所有的名字存到前端
        instituteList: [], // 把所有的机构存到前端
        teacherList: [], // 把所有教师的名字存到前端
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        role: 0, // 定义 0为总管理员、1为学工处、2为学院老师、3为辅导员老师、4为学生
        isSearchCollapse: false,
        isImportCollapse: false,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        loading: false,
        isupdate: true,
        options: [{
          value: '未评价',
          label: '未评价'
        }, {
          value: '已评价',
          label: '已评价'
        }]
      }
    },
    components: {
      StudentEvaluateForm
    },
    activated () {
      this.getUserInfo()
    },
    mounted () {
      // 获取所有的学生、老师姓名，机构
      this.$http({
        url: '/studentevaluate/studentEvaluate/queryPinyin',
        method: 'get'
      }).then(({data}) => {
        if (data && data.success) {
          this.nameList = data.nameList
          this.instituteList = data.instituteList
          this.teacherList = data.teacherList
        }
      })
    },
    methods: {
      test () {
        // var params = {
        //   'studentId': '201908090601'
        // }
        this.$utils.download('/moredumain/studentMoreduMain/exportByIndividual')
        // this.$utils.download('/test/test/test')
      },
      guide () {
        this.$tours['myTour'].start()
      },
      clickQuery (institute) {
        this.resetSearch()
        this.searchForm.institute = institute
        this.getUserInfo()
      },
      clickTeacher (teacher) {
        this.resetSearch()
        this.searchForm.teacher = teacher
        this.getUserInfo()
      },
      queryName (queryString, cb) {
        var restaurants = this.nameList
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
        // 调用 callback 返回建议列表的数据
        cb(results)
      },
      queryInstitute (queryString, cb) {
        var restaurants = this.instituteList
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
        // 调用 callback 返回建议列表的数据
        cb(results)
      },
      queryTeacher (queryString, cb) {
        var restaurants = this.teacherList
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
        // 调用 callback 返回建议列表的数据
        cb(results)
      },
      // 进行拼音搜索
      createFilter (queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0 || pinyin.match(restaurant.value, queryString))
        }
      },
      // 选择的内容
      handleSelect (item) {
      },
      createfa () {
        this.$http({
          url: `/studentevaluate/studentEvaluate/createfa`,
          method: 'post'
        }).then(({data}) => {
          this.loading = false
        })
      },
      // 获取当前登录用户信息 为了提交
      getUserInfo () {
        this.$http({
          url: '/sys/user/info',
          method: 'get'
        }).then(({data}) => {
          if (data.success) {
            if (data.user.roleIds === '52211ea85fbe423a8f3d11c39e09dc51') {
              this.role = 2
              this.officeName = data.user.office.name
              this.searchForm.institute = this.officeName
            }
            if (data.user.roleIds === '2a21eb97b6dc4b1ba64ca654d38be348') {
              this.role = 3
              this.officeName = data.user.office.name
              this.reviewer = data.user.name
              this.searchForm.institute = this.officeName
              this.searchForm.teacher = this.reviewer
            }
            if (data.user.roleIds === 'b930ac75924c47f99ac92c6fc8c3accb') {
              this.role = 4
              this.searchForm.name = data.user.name
            }
            this.refreshList()
          }
        })
      },
      // 一键导入
      imp () {
        this.$confirm(`确定要导入所有学生吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/studentevaluate/studentEvaluate/allin',
            method: 'get'
          }).then(({data}) => {
            if (data && data.success) {
              // this.dataList = data.page.list
              this.refreshList()
            }
          })
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/studentevaluate/studentEvaluate/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.studentEvaluateForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.studentEvaluateForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.studentEvaluateForm.init('view', id)
      },
      reEvaluate (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定打回所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/studentevaluate/studentEvaluate/reEvaluate',
            method: 'post',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/studentevaluate/studentEvaluate/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 删除所有数据
      delall () {
        this.$confirm(`确定删除所有数据吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/studentevaluate/studentEvaluate/delall',
            method: 'delete'
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/studentevaluate/studentEvaluate/import/template')
      },
      handleClose (done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done()
          })
          .catch(_ => {})
      },

      exportExcel () {
        let params = {
          ...this.searchForm
        }
        this.$utils.download('/studentevaluate/studentEvaluate/exportnew', params)
        this.dialogVisible = false
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
<style lang="less" scoped>
  /deep/ .el-input__inner  {
    height: 32px;
  }
</style>
